package com.kexun.lxj;

public class Demo07 {
    public static void main(String[] args) {
        Runnable r =new Ticket2();

        //创建四个线程,卖同一个资源
        Thread t1 = new Thread(r);
        Thread t2 = new Thread(r);
        Thread t3 = new Thread(r);
        Thread t4= new Thread(r);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}


/*成员变量在方法内 就不存在安全问题  每个线程的资源并没有共享*/
class Ticket2 implements Runnable {

    @Override
    public  void run() {
         int i = 5;
        while (true) {

                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (i > 0) {
                    try {
                        System.out.println("这是" + Thread.currentThread().getName() + "卖出的第" + i-- + "张票");
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println(Thread.currentThread().getName()+"卖完了,关闭卖票窗口");
                    break;
                }
            }

    }
}